package com.gitee.wsl.compose.animation

import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.animateBounds
import androidx.compose.animation.animateColor
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector
import androidx.compose.animation.core.AnimationVector2D
import androidx.compose.animation.core.Easing
import androidx.compose.animation.core.FiniteAnimationSpec
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.animateInt
import androidx.compose.animation.core.animateValue
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.animation.expandIn
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.shrinkOut
import androidx.compose.animation.togetherWith
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.BiasAlignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.layout.LookaheadScope
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType



object A{
    object Easing{
        object In

        object Out

        object InOut

        object OutIn
    }

    object Duration {
        const val QUICK = 83
        const val SHORT = 187
        const val MEDIUM = 333
        const val LONG = 500
        const val VERY_LONG = 667
    }

    object Converter
}

/**
 * var text by remember { mutableStateOf("Upload") }
 * val textAlpha = remember { mutableStateOf(1.0f) }
 * val backgroundColor = remember { mutableStateOf(Color.Blue) }
 * val boxWidth = remember { mutableStateOf(180.dp) }
 * val progressAlpha = remember { mutableStateOf(0.0f) }
 * val progress = remember { mutableStateOf(0) }
 *
 * val endAnimatorSet = remember {
 *      val animatorSet = AnimatorSet()
 *      val widthAnimator = animatorOfDp(boxWidth, arrayOf(circleSize, originWidth))
 *      val centerAnimator = animatorOfFloat(progressAlpha, 1f, 0f)
 *      val textAnimator = animatorOfFloat(textAlpha, 0f, 1f)
 *      val colorAnimator = animatorOfColor(backgroundColor, arrayOf(Color.Blue, Color.Red))
 *      animatorSet.playTogether(widthAnimator, centerAnimator, textAnimator, colorAnimator)
 *      animatorSet.addListener(onStart = {
 *      text = "Success"
 *      })
 *     animatorSet
 * }
 *
 * val progressAnimator = remember {
 *      val animator = animatorOfInt(progress, 0, 100)
 *      animator.duration = 1000
 *      animator.addListener(onEnd = {
 *      endAnimatorSet.start()
 *      })
 *      animator
 * }
 *
 * val uploadStartAnimator = remember {
 *      val animatorSet = AnimatorSet()
 *      val widthAnimator = animatorOfDp(boxWidth, arrayOf(originWidth, circleSize))
 *      val textAnimator = animatorOfFloat(textAlpha, 1f, 0.0f)
 *      val colorAnimator = animatorOfColor(backgroundColor, arrayOf(Color.Blue, Color.Gray))
 *      val centerAlphaAnimator = animatorOfFloat(progressAlpha, 0.0f, 1f)
 *      animatorSet.playTogether(widthAnimator, textAnimator, colorAnimator, centerAlphaAnimator)
 *      animatorSet.addListener(onEnd = {
 *      progressAnimator.start()
 *      })
 *      animatorSet
 * }
 *
 */


@Composable
fun <T> A.Crossfade(targetState: T,
                     modifier: Modifier = Modifier,
                     animationSpec: FiniteAnimationSpec<Float> = tween(),
                     label: String = "Crossfade",
                     content: @Composable (T) -> Unit
                ) = androidx.compose.animation.Crossfade(targetState,modifier, animationSpec, label, content)


@Composable
fun <S> A.AnimatedContent(
    targetState: S,
    modifier: Modifier = Modifier,
    transitionSpec: AnimatedContentTransitionScope<S>.() -> ContentTransform = {
        (fadeIn(animationSpec = tween(220, delayMillis = 90)) +
                scaleIn(initialScale = 0.92f, animationSpec = tween(220, delayMillis = 90)))
            .togetherWith(fadeOut(animationSpec = tween(90)))
    },
    contentAlignment: Alignment = Alignment.TopStart,
    label: String = "AnimatedContent",
    contentKey: (targetState: S) -> Any? = { it },
    content: @Composable() AnimatedContentScope.(targetState: S) -> Unit
) = androidx.compose.animation.AnimatedContent(targetState,modifier, transitionSpec, contentAlignment, label, contentKey, content)

@Composable
fun <S> A.AnimatedContent(
    targetState: S,
    modifier: Modifier = Modifier,
    enter: EnterTransition = (fadeIn(animationSpec = tween(220, delayMillis = 90)) + scaleIn(initialScale = 0.92f, animationSpec = tween(220, delayMillis = 90))),
    exit: ExitTransition = fadeOut(animationSpec = tween(90)),
    contentZIndex:Float = 0f,
    contentAlignment: Alignment = Alignment.TopStart,
    label: String = "AnimatedContent",
    contentKey: (targetState: S) -> Any? = { it },
    content: @Composable() AnimatedContentScope.(targetState: S) -> Unit
) = AnimatedContent(targetState, modifier, { enter.togetherWith(exit).apply { targetContentZIndex = contentZIndex }  }, contentAlignment, label, contentKey, content)


//@Composable
//fun A.AnimatedVisibility(
//                          visibleState: MutableTransitionState<Boolean>,
//                          modifier: Modifier = Modifier,
//                          enter: EnterTransition = fadeIn() + expandIn(),
//                          exit: ExitTransition = fadeOut() + shrinkOut(),
//                          label: String = "AnimatedVisibility",
//                          content: @Composable() AnimatedVisibilityScope.() -> Unit
//) = androidx.compose.animation.AnimatedVisibility(
//    visibleState,modifier, enter, exit, label, content)
//)

/**
 * 边界动画 Modifier.animateBounds
 * 供了一个“预计算”布局阶段。在这个阶段，Compose 可以知道 Composable 在 下一次 布局传递中的目标大小和位置。
 *
 * Modifier
 *    .align(Alignment.Center) // 居中对齐
 *    // 根据 expanded 状态改变宽度和偏移量
 *    .width(if (expanded) 180.dp else 110.dp)
 *    .offset(x = if (expanded) 0.dp else 100.dp)
 *    // !!! 核心：使用 animateBounds Modifier !!!
 *    .animateBounds(
 *      lookaheadScope = this@LookaheadScope, // 传入 LookaheadScope 实例
 *     )
 *    .background(Color.LightGray, shape = RoundedCornerShape(12.dp))
 *    .height(50.dp)
 *
 */
@Composable
fun A.LookaheadScope(content: @Composable LookaheadScope.() -> Unit) = androidx.compose.ui.layout.LookaheadScope(content)


